<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="订单主体ID" prop="headerId">
      <el-input v-model="dataForm.headerId" placeholder="订单主体ID"></el-input>
    </el-form-item>
    <el-form-item label="商品ID" prop="commodityId">
      <el-input v-model="dataForm.commodityId" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="宽" prop="width">
      <el-input v-model="dataForm.width" placeholder="宽"></el-input>
    </el-form-item>
    <el-form-item label="高" prop="height">
      <el-input v-model="dataForm.height" placeholder="高"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="count">
      <el-input v-model="dataForm.count" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="方向" prop="direction">
      <el-input v-model="dataForm.direction" placeholder="方向"></el-input>
    </el-form-item>
    <el-form-item label="面积，平方数" prop="squares">
      <el-input v-model="dataForm.squares" placeholder="面积，平方数"></el-input>
    </el-form-item>
    <el-form-item label="板材，材料ID" prop="materialId">
      <el-input v-model="dataForm.materialId" placeholder="板材，材料ID"></el-input>
    </el-form-item>
    <el-form-item label="颜色ID" prop="colorId">
      <el-input v-model="dataForm.colorId" placeholder="颜色ID"></el-input>
    </el-form-item>
    <el-form-item label="销售金额" prop="salesAmount">
      <el-input v-model="dataForm.salesAmount" placeholder="销售金额"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态，0：停用，1：启用" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态，0：停用，1：启用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          headerId: '',
          commodityId: '',
          commodityName: '',
          commodityNameValue: '',
          commodityPrice: 0,
          width: '',
          height: '',
          count: 0,
          direction: '',
          squares: '',
          materialId: '',
          materialCode: '',
          materialName: '',
          materialPrice: 0,
          measurementId: '',
          minSquares: '',
          colorId: '',
          colorCode: '',
          colorName: '',
          colorPrice: 0,
          salesAmount: '',
          remark: '',
          status: '',
          directionNum: ''
        },
        dataRule: {
          headerId: [
            { required: true, message: '订单主体ID不能为空', trigger: 'blur' }
          ],
          commodityId: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          width: [
            { required: true, message: '宽不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '高不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          direction: [
            { required: true, message: '方向不能为空', trigger: 'blur' }
          ],
          squares: [
            { required: true, message: '面积，平方数不能为空', trigger: 'blur' }
          ],
          materialId: [
            { required: true, message: '板材，材料ID不能为空', trigger: 'blur' }
          ],
          colorId: [
            { required: true, message: '颜色ID不能为空', trigger: 'blur' }
          ],
          salesAmount: [
            { required: true, message: '销售金额不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sales/orderdetail/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.headerId = data.orderDetail.headerId
                this.dataForm.commodityId = data.orderDetail.commodityId
                this.dataForm.width = data.orderDetail.width
                this.dataForm.height = data.orderDetail.height
                this.dataForm.count = data.orderDetail.count
                this.dataForm.direction = data.orderDetail.direction
                this.dataForm.squares = data.orderDetail.squares
                this.dataForm.materialId = data.orderDetail.materialId
                this.dataForm.colorId = data.orderDetail.colorId
                this.dataForm.salesAmount = data.orderDetail.salesAmount
                this.dataForm.remark = data.orderDetail.remark
                this.dataForm.status = data.orderDetail.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sales/orderdetail/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'headerId': this.dataForm.headerId,
                'commodityId': this.dataForm.commodityId,
                'width': this.dataForm.width,
                'height': this.dataForm.height,
                'count': this.dataForm.count,
                'direction': this.dataForm.direction,
                'directionNum': this.dataForm.directionNum,
                'squares': this.dataForm.squares,
                'materialId': this.dataForm.materialId,
                'colorId': this.dataForm.colorId,
                'salesAmount': this.dataForm.salesAmount,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

